package com.hooper.controller;

import com.hooper.common.PageResult;
import com.hooper.common.Result;
import com.hooper.model.system.SysUser;
import com.hooper.model.vo.*;
import com.hooper.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/04/01/16:01
 */
@Api(tags = "用户管理")
@Slf4j
@RestController
@RequestMapping("/admin/system/user")
public class SysUserController {


    @Resource
    private SysUserService userService;

    @ApiOperation("获取所有用户")
    @GetMapping("/list")
    public Result<List<SysUser>> getAll() {
        return Result.ok(userService.getAllRoles());
    }

    @ApiOperation("分页查询用户")
    @PostMapping("page/{currentPage}/{pageSize}")
    public Result<PageResult<SysUser>> page(@PathVariable Integer currentPage,
                                            @PathVariable Integer pageSize,
                                            @RequestBody(required = false) SysUserQueryVo queryVo) {
        log.info("接收到请求，参数：currentPage={},pageSize={},queryVo={}", currentPage, pageSize, queryVo);
        return Result.ok(userService.pageByConditions(currentPage, pageSize, queryVo));
    }

    @ApiOperation("更新用户")
    @PutMapping("update")
    public Result<Void> update(@RequestBody SysUser newUser) {
        userService.updateUserById(newUser);
        return Result.ok();
    }

    @ApiOperation("新增用户")
    @PostMapping("save")
    public Result<Void> save(@RequestBody SysUser sysUser) {
        userService.saveUser(sysUser);
        return Result.ok();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result<Void> delById(@PathVariable Long id) {
        userService.delUserById(id);
        return Result.ok();
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("/del-batch")
    public Result<Void> delBatch(@RequestBody List<Long> ids) {
        log.info("接收到请求，参数：ids={}", ids);
        userService.delBatchByIds(ids);
        return Result.ok();
    }

    @ApiOperation("通过id获取用户")
    @GetMapping("get/{id}")
    public Result<SysUser> getById(@PathVariable Long id) {
        return Result.ok(userService.getById(id));
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        userService.changeUserStatus(id, status);
        return Result.ok();
    }
}
